package com.leetcode.LC;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class LC349 {
    public int[] intersection(int[] nums1, int[] nums2) {

        int[] ints = new int[nums1.length>nums2.length?nums2.length:nums1.length];
        int count=0;
        Set<Integer> set = new HashSet<Integer>();
        for (int i = 0; i < nums1.length; i++) {
            if(!set.contains(nums1[i])){
                set.add(nums1[i]);
            }
        }

        for (int i = 0; i < nums2.length; i++) {
            if (set.contains(nums2[i])){
                ints[count]=nums2[i];
                count++;
                set.remove(nums2[i]);
            }
        }
        return Arrays.copyOf(ints,count);
    }
    public static void main(String[] args) {
        for (int i : new LC349().intersection(new int[]{4,9,5}, new int[]{9,4,9,8,4})) {
            System.out.print(i+",");
        }
    }
}
